package com.ruiyi.subway.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;

public class SystemException {
	
	protected Logger log = LoggerFactory.getLogger(getClass());
	
	private static SystemException systemException=null;
	
	public static void  errorLog(Class clazz,String methodName,Exception ex){
		if (systemException==null){
			systemException=new SystemException();
		}
		systemException.writeLog(clazz, methodName, ex);
	}
	 
	public void writeLog(Class clazz,String methodName,Exception ex) {
		log.error("ERROR 类名："+clazz.getName());
		log.error("ERROR 方法名："+methodName);
		if (ex!=null){
			String[] errorMessage=exception(ex); 
			StringBuffer errContent=new StringBuffer();
			errContent.append(errorMessage[0]).append("\n");
			errContent.append(errorMessage[1]).append(":").append(ex.getMessage()).append("\n");
			StackTraceElement[] errTrace=ex.getStackTrace();
			for(int i=0;i<errTrace.length;i++){
				StackTraceElement error=errTrace[i];
				errContent.append("at ").append(error.getClassName()).append(".").append(error.getMethodName()).append("  ");
				errContent.append("(").append(error.getFileName()).append(":").append(error.getLineNumber()).append(")").append("\n");
			}
			
			log.error("ERROR 详细信息：");
			log.error(errContent.toString());
		}
	}
	
	/**
	 * 异常信息
	 * @param e
	 * @return
	 * @throws Exception
	 */
	private String[] exception(Throwable e) {
		String[] msg = new String[2];
		if (e instanceof DataAccessException) {
			msg[0] = "数据库操作失败！";
			msg[1] = "DataAccessException";
		} else if (e instanceof NullPointerException) {
			msg[0] = "调用了未经初始化的对象或者是不存在的对象！";
			msg[1] = "NullPointerException";
		} else if (e instanceof UnsupportedEncodingException) {
			msg[0] = "不支持字符编码！";
			msg[1] = "UnsupportedEncodingException";
		} else if (e instanceof IOException) {
			msg[0] = "IO异常！";
			msg[1] = "IOException";
		} else if (e instanceof ClassNotFoundException) {
			msg[0] = "指定的类不存在！";
			msg[1] = "ClassNotFoundException";
		} else if (e instanceof ArithmeticException) {
			msg[0] = "数学运算异常！";
			msg[1] = "ArithmeticException";
		} else if (e instanceof ArrayIndexOutOfBoundsException) {
			msg[0] = "数组下标越界！";
			msg[1] = "ArrayIndexOutOfBoundsException";
		} else if (e instanceof IllegalArgumentException) {
			msg[0] = "方法的参数错误！";
			msg[1] = "IllegalArgumentException";
		} else if (e instanceof ClassCastException) {
			msg[0] = "类型强制转换错误！";
			msg[1] = "ClassCastException";
		} else if (e instanceof SecurityException) {
			msg[0] = "违背安全原则异常！";
			msg[1] = "SecurityException";
		} else if (e instanceof SQLException) {
			msg[0] = "操作数据库异常！";
			msg[1] = "SQLException";
		} else if (e instanceof Exception) {
			msg[0] = "程序内部错误，操作失败！";
			msg[1] = "Exception";
		} else if (e instanceof Throwable) {
			msg[0] = "Java虚拟机发生了内部错误！";
			msg[1] = "Throwable";
		} else {
			msg[0] = "未知错误！";
			msg[1] = "";
		}
		return msg;
	}
	
}

